package com.shangcheng.dubbo.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.SmsCouponMapper;
import com.shangcheng.model.domain.SmsCoupon;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponApiImpl implements SmsCouponApi {
    @Autowired
    private SmsCouponMapper couponMapper;

    @Override
    public IPage<SmsCoupon> list(String name, Integer type, Integer pageNum, Integer pageSize) {
        IPage<SmsCoupon> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(type), SmsCoupon::getType, type);
        wrapper.like(StrUtil.isNotBlank(name), SmsCoupon::getName, name);
        return couponMapper.selectPage(page, wrapper);
    }

    @Override
    public SmsCoupon create(SmsCoupon smsCoupon) {
        couponMapper.insert(smsCoupon);
        return smsCoupon;
    }

    @Override
    public SmsCoupon findById(Integer id) {
        return couponMapper.selectById(id);
    }

    @Override
    public void updateByCouponId(SmsCoupon coupon) {
        couponMapper.updateById(coupon);
    }

    @Override
    public void deleteByCouponId(Integer id) {
        couponMapper.deleteById(id);
    }
}
